﻿using AutoMapper;
using B.S.ProductionProject.Domain;
using B.S.ProductionProject.ErrorCode;
using B.S.ProductionProject.Write.WebAPI.Applications.Commands.ProductionScheduleDetailCommand;
using MediatR;
using SqlSugar;

namespace B.S.ProductionProject.Write.WebAPI.Applications.CommandHandlers.ProductionScheduleDetailCommandHandler
{
    public class UpdateProductionScheduleDetailCommandHandler : IRequestHandler<UpdateProductionScheduleDetailCommand, APIResult<object>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public UpdateProductionScheduleDetailCommandHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(UpdateProductionScheduleDetailCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.code = APIResultCode.success;
            result.msg = "修改成功";

            var model = mapper.Map<ProductionScheduleDetail>(request);
            var list = db.Queryable<ProductionScheduleDetail>().Where(p => p.ProductionScheduleDetailId == request.ProductionScheduleDetailId);
            if (list.Count() != 0)
            {
                await db.Updateable<ProductionScheduleDetail>(model).ExecuteCommandAsync();
            }

            return await Task.FromResult(result);
        }
    }
}
